package m4;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Scanner;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");  // wht6.cn
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        f.setVirtualHost("wht"); // 自定义空间
        Channel c = f.newConnection().createChannel();
        // 1.创建随机队列 2.创建交换机 3.绑定，设置绑定键
        String queue = UUID.randomUUID().toString();
        c.queueDeclare(queue, false, true, true, null);
        c.exchangeDeclare("direct_logs", BuiltinExchangeType.DIRECT);
        System.out.println("输入绑定键，用空格隔开："); // aa   bb    cc
        String s = new Scanner(System.in).nextLine();
        String[] a = s.split("\\s+");//   \s空白字符    +一到多个
        for (String k : a) {
            c.queueBind(queue, "direct_logs", k);
        }
        // 从队列接收，处理消息
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            String msg = new String(message.getBody());
            String key = message.getEnvelope().getRoutingKey(); //消息上携带的路由键
            System.out.println(key + " -- " + msg);
        };
        CancelCallback cancelCallback = consumerTag -> {};
        c.basicConsume(queue, true, deliverCallback, cancelCallback);
    }
}
